package com.itheima.service.Impl;

import cn.hutool.core.lang.Snowflake;
import com.itheima.dto.DishDto;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.FlavorMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishService;
import com.itheima.vo.DishListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorMapper flavorMapper;

    @Override
    public boolean addDish(DishDto dto) {
        String categoryId = dto.getCategoryId();
        if (categoryId == null) {
            return false;
        }
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        BeanUtils.copyProperties(dto, dish);
        Snowflake snowflake = new Snowflake();
        String dishId = snowflake.nextIdStr();
        dish.setId(dishId);
        int i = dishMapper.insertDish(dish);
        if (i > 0) {
            List<DishFlavor> flavors = dto.getFlavors();
            for (DishFlavor flavor : flavors) {
                if (flavor != null) {
                    String flavorId = new Snowflake().nextIdStr();
                    flavor.setId(flavorId);
                    flavor.setDishId(dishId);
                    flavorMapper.insertFlavor(flavor);
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyDish(DishDto dto) {
        String dishId = dto.getId();
        String categoryId = dto.getCategoryId();
        if (categoryId == null) {
            return false;
        }
        Dish dish = new Dish();
        dish.setCategoryId(categoryId + "");
        BeanUtils.copyProperties(dto, dish);
        int i = dishMapper.updateDish(dish);
        if (i > 0) {
            flavorMapper.deleteFlavors(dishId);
            List<DishFlavor> flavors = dto.getFlavors();
            for (DishFlavor flavor : flavors) {
                if (flavor != null) {
                    String flavorId = new Snowflake().nextIdStr();
                    flavor.setId(flavorId);
                    flavor.setDishId(dishId);
                    flavorMapper.insertFlavor(flavor);
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteDish(String[] dishIds) {
        flavorMapper.deleteFlavors(dishIds);
        return dishMapper.deleteDish(dishIds) > 0;
    }

    @Override
    public List<DishListVo> getDishList(String categoryId) {
        List<DishListVo> dishListVos = dishMapper.selectDishList(categoryId);
        dishListVos.stream().filter(Objects::nonNull).forEach(dishListVo -> {
            String dishId = dishListVo.getId();
            List<DishFlavor> dishFlavors = flavorMapper.selectByDishId(dishId);
            dishListVo.setFlavors(dishFlavors);
        });
        return dishListVos;
    }

    @Override
    public List<DishListVo> getDishPage(String name, Integer page, Integer pageSize) {
        int index = (page - 1) * pageSize;
        return dishMapper.selectDishPage(name, index, pageSize);
    }

    @Override
    public DishDto getDishById(String id) {
        DishDto dishDto = dishMapper.selectDishById(id);
        return dishDto;
    }

    @Override
    public boolean modifyStatus(int status, String... ids) {
        return dishMapper.updateStatusById(status, ids) == ids.length;
    }


}
